package tani.bnn.com.mbilling.core;

import java.util.ArrayList;
import java.util.List;

import tani.bnn.com.mbilling.utility.Utility;

public class Product {
	private	int		id;
	private	String	name;
	private	String	searchCode;
	private	String	model;
	private int		stock;
	private boolean	deleted;
	private	String	companyName;
	private	String	categoryName;
	private List<ProductSaleDetail> sales;
	private List<ProductPurchaseDetail> purchases;
	private List<ProductPurchaseDetail> stocks;
	private List<QuantityPriceDetail> quantityPriceDetails = new ArrayList<QuantityPriceDetail>();
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the searchCode
	 */
	public String getSearchCode() {
		return searchCode;
	}
	/**
	 * @param searchCode the searchCode to set
	 */
	public void setSearchCode(String searchCode) {
		this.searchCode = searchCode;
	}
	/**
	 * @return the model
	 */
	public String getModel() {
		return model;
	}
	/**
	 * @param model the model to set
	 */
	public void setModel(String model) {
		this.model = model;
	}
	/**
	 * @return the stock
	 */
	public int getStock() {
		return stock;
	}
	/**
	 * @param stock the stock to set
	 */
	public void setStock(int stock) {
		this.stock = stock;
	}
	/**
	 * @return the deleted
	 */
	public boolean isDeleted() {
		return deleted;
	}
	/**
	 * @param deleted the deleted to set
	 */
	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
	}
	/**
	 * @return the companyName
	 */
	public String getCompanyName() {
		return companyName;
	}
	/**
	 * @param companyName the companyName to set
	 */
	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
	/**
	 * @return the categoryName
	 */
	public String getCategoryName() {
		return categoryName;
	}
	/**
	 * @param categoryName the categoryName to set
	 */
	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}
	/**
	 * @return the sales
	 */
	public List<ProductSaleDetail> getSales() {
		if(sales == null){
			sales = new ArrayList<ProductSaleDetail>();
		}
		return sales;
	}
	/**
	 * @param sales the sales to set
	 */
	public void setSales(List<ProductSaleDetail> sales) {
		this.sales = sales;
	}
	/**
	 * @return the purchases
	 */
	public List<ProductPurchaseDetail> getPurchases() {
		if(purchases == null){
			purchases = new ArrayList<ProductPurchaseDetail>();
		}
		return purchases;
	}
	/**
	 * @param purchases the purchases to set
	 */
	public void setPurchases(List<ProductPurchaseDetail> purchases) {
		this.purchases = purchases;
	}
	/**
	 * @return the quantityPriceDetails
	 */
	public List<QuantityPriceDetail> getQuantityPriceDetails() {
		return quantityPriceDetails;
	}
	/**
	 * @param quantityPriceDetails the all to set
	 */
	public void setQuantityPriceDetails(List<QuantityPriceDetail> quantityPriceDetails) {
		this.quantityPriceDetails = quantityPriceDetails;
	}
	/**
	 * @return the stocks
	 */
	public List<ProductPurchaseDetail> getStocks() {
		if(stocks == null){
			stocks = new ArrayList<ProductPurchaseDetail>();
		}
		return stocks;
	}
	/**
	 * @param stocks the stocks to set
	 */
	public void setStocks(List<ProductPurchaseDetail> stocks) {
		this.stocks = stocks;
	}
	public void setProfitLoss(){
		int quantity = 0;
		List<ProductSaleDetail> newSalesList = new ArrayList<ProductSaleDetail>();
		List<ProductSaleDetail> usedSalesList = new ArrayList<ProductSaleDetail>();
		if(sales == null || sales.isEmpty()){
			if(purchases != null && !purchases.isEmpty()){
				getStocks().addAll(purchases);
			}
		} else if(purchases == null || purchases.isEmpty()){
			for(ProductSaleDetail sale: sales){
				sale.setProfitLoss(20);
				newSalesList.add(sale);
			}
		} else {
			for(ProductPurchaseDetail purchase: purchases){
				if(!getSales().isEmpty()){
					quantity += purchase.getQuantity();
					for(ProductSaleDetail sale: sales){
						int saleQuantity = sale.getQuantity();
						purchase.setProfitLoss(0);
						if(quantity >= (-1*saleQuantity) ){
							quantity += saleQuantity;
							if(sale.getPrice() == 0 || purchase.getPrice() == 0){
								sale.setProfitLoss(20);
							} else {
								sale.setProfitLoss(sale.getPrice() - purchase.getPrice());
							}
							newSalesList.add(sale);
							usedSalesList.add(sale);
						} else {
							if(quantity > 0){
								ProductSaleDetail newSale = new ProductSaleDetail();
								newSale.setQuantity(-1*quantity);
								newSale.setPrice(sale.getPrice());
								newSale.setProfitLoss(sale.getPrice() - purchase.getPrice());
								sale.setQuantity(saleQuantity + quantity);
								newSalesList.add(newSale);
								quantity = 0;
							}
							sales.removeAll(usedSalesList);
							break;
						}
					}
					sales.removeAll(usedSalesList);
					if (quantity > 0) {
						ProductPurchaseDetail newStock = new ProductPurchaseDetail();
						newStock.setQuantity(quantity);
						newStock.setProfitLoss(0);
						newStock.setPrice(purchase.getPrice());
						getStocks().add(newStock);
					}
				} else {
					getStocks().add(purchase);
				}
			}
		}
		setSales(newSalesList);
		quantityPriceDetails.addAll(getPurchases());
		quantityPriceDetails.addAll(newSalesList);
	}
	public void setProfitLossByDate(){
		int quantity = 0;
		List<ProductSaleDetail> newSalesList = new ArrayList<ProductSaleDetail>();
		List<ProductSaleDetail> usedSalesList = new ArrayList<ProductSaleDetail>();
		if(sales == null || sales.isEmpty()){
			if(purchases != null && !purchases.isEmpty()){
				for(ProductPurchaseDetail purchase: purchases){
					getStocks().add(purchase);
					quantity = purchase.getQuantity();
				}
				setStock(quantity);
			}
		} else if(purchases == null || purchases.isEmpty()){
			for(ProductSaleDetail sale: sales){
				sale.setProfitLoss(Utility.getAppUtility().getDefaultProfit());
				quantity = sale.getQuantity();
				newSalesList.add(sale);
			}
			setStock(quantity);
		} else {
			for(ProductPurchaseDetail purchase: purchases){
				quantity += purchase.getQuantity();
				if(!getSales().isEmpty()){
					for(ProductSaleDetail sale: sales){
						int saleQuantity = sale.getQuantity();
						purchase.setProfitLoss(0);
						if(purchase.getSopDate().after(sale.getSopDate())){
							if(quantity > (-1 * saleQuantity)){
								sale.setProfitLoss(21);
								usedSalesList.add(sale);
								newSalesList.add(sale);
								quantity += saleQuantity;
							} else if (quantity > 0) {
								ProductSaleDetail newSale = new ProductSaleDetail();
								newSale.setQuantity(-1 * quantity);
								newSale.setPrice(sale.getPrice());
								newSale.setProfitLoss(21);
								newSale.setSopDate(sale.getSopDate());
								sale.setQuantity(saleQuantity + quantity);
								newSalesList.add(newSale);
								quantity = 0;
							}
						} else {
							if (quantity >= (-1 * saleQuantity)) {
								quantity += saleQuantity;
								if (sale.getPrice() == 0
										|| purchase.getPrice() == 0) {
									sale.setProfitLoss(21);
								} else {
									sale.setProfitLoss(sale.getPrice()
											- purchase.getPrice());
								}
								newSalesList.add(sale);
								usedSalesList.add(sale);
							} else {
								if (quantity > 0) {
									ProductSaleDetail newSale = new ProductSaleDetail();
									newSale.setQuantity(-1 * quantity);
									newSale.setPrice(sale.getPrice());
									newSale.setProfitLoss(sale.getPrice()
											- purchase.getPrice());
									newSale.setSopDate(sale.getSopDate());
									sale.setQuantity(saleQuantity + quantity);
									newSalesList.add(newSale);
									quantity = 0;
								}
							}
						}
						if(quantity == 0){
							sales.removeAll(usedSalesList);
							usedSalesList.clear();
							break;
						}
					}
					sales.removeAll(usedSalesList);
					if (quantity > 0) {
						ProductPurchaseDetail newStock = new ProductPurchaseDetail();
						newStock.setQuantity(quantity);
						newStock.setProfitLoss(0);
						newStock.setPrice(purchase.getPrice());
						getStocks().add(newStock);
					}
				} else {
					getStocks().add(purchase);
				}
			}
		}
		setStock(quantity);
		ProductSaleDetail prevSale = null;
		List<ProductSaleDetail> dupList = new ArrayList<ProductSaleDetail>();
		for(ProductSaleDetail sale: newSalesList){
			if(prevSale != null){
				if(prevSale.getSopDate().equals(sale.getSopDate()) && prevSale.getPrice() == sale.getPrice()
						&& prevSale.getProfitLoss() == sale.getProfitLoss()){
					prevSale.setQuantity(prevSale.getQuantity() + sale.getQuantity());
					dupList.add(sale);
				}
			}
			prevSale = sale;
		}
		if(!dupList.isEmpty()){
			newSalesList.removeAll(dupList);
		}
		setSales(newSalesList);
		quantityPriceDetails.addAll(getPurchases());
		quantityPriceDetails.addAll(newSalesList);
	}
}